﻿Partial Public Class PersonExplorer
    ''' <summary>
    ''' This Sub will create a SQL User with Windows Authentification ()
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Overridable Sub Create_SQLUser_SSPI(ByVal sender As Object, ByVal e As EventArgs)
        Me.Cursor = Cursors.WaitCursor

        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim sqlcmd As SqlClient.SqlCommand = MetaQuery.GetSPCmdByMetaName("SSPCreateUser")
        Dim strDomain As String = MetaValue.GetMetaValueByCode("OPT_SYSMAILDOMAIN").Value
        strDomain = strDomain.Substring(0, strDomain.IndexOf(".")).ToUpper

        Dim ml As MetaList = New MetaList("TypeLogin")
        Dim loginTypeValue As String = ml.MetaListValues(IdRepository.META_TYPELOGIN_SSPI).ListValueCd

        ml = New MetaList("SQLRoles")
        Dim sqlRoleValue As String = ml.MetaListValues(IdRepository.META_SQLROLES_SYSTEMBOXUSERS).ListValueCd

        Try
            sqlcmd.Parameters("@Login").Value = String.Format("{0}\{1}", strDomain, aDataRow("UserPrincipalName").ToString.Substring(0, aDataRow("UserPrincipalName").ToString.IndexOf("@")).ToLower)
            sqlcmd.Parameters("@Password").Value = Nothing
            sqlcmd.Parameters("@SqlRole").Value = sqlRoleValue
            sqlcmd.Parameters("@ApplicationRole").Value = "Public"
            sqlcmd.Parameters("@Type").Value = CBool(loginTypeValue)

            sqlcmd.ExecuteReader(CommandBehavior.CloseConnection)
            MessageWindow.ShowMessage("Usager SQL créer avec succès.")

        Catch sqlex As SqlClient.SqlException
            MessageWindow.ShowMessage(sqlex)
        Finally
            If sqlcmd IsNot Nothing Then
                sqlcmd.Connection.Close()
                sqlcmd.Dispose()
            End If
        End Try

        Me.Cursor = Cursors.Default
    End Sub

    Public Overridable Sub Create_SQLUser_SQL(ByVal sender As Object, ByVal e As EventArgs)
        Me.Cursor = Cursors.WaitCursor
        Try
            Dim selectedRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)

            Me.Cursor = Cursors.WaitCursor
            Dim newForm As XtraForm = New XtraForm() With {.Text = "Create User", .Width = 800}
            Dim gridSP As GridExtSP = New GridExtSP("SSPCreateUser") With {.Dock = DockStyle.Fill}
            gridSP.SaveCMD = AddressOf CreateUser

            AddHandler gridSP.AfterLoad, AddressOf AddRowParameter

            newForm.Controls.Add(gridSP)
            newForm.ShowDialog()
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            MessageWindow.ShowMessage(ex, MsgBoxStyle.Information)
        End Try

        Me.Cursor = Cursors.Default
    End Sub

    Public Overridable Function CreateUser(ByVal aMetaDataTable As MetaDataTable) As Boolean
        Try

            If aMetaDataTable.ApplyChanges(reload:=False) > 0 Then
                MessageWindow.ShowMessage("Successfully Create User.", MsgBoxStyle.Information)
            Else
                MessageWindow.ShowMessage("Problem to create user, please check your value", MsgBoxStyle.Information)
            End If

        Catch ex As Exception
            MessageWindow.ShowMessage(ex, MsgBoxStyle.Information)
        End Try
        Return True
    End Function

    Public Overridable Sub AddRowParameter(sender As Object, e As System.EventArgs)
        Try
            Dim aGrid As GridExtSP = CType(sender, GridExtSP)

            Dim arow As DataRow = aGrid.MetaDataTable.NewRow()
            'assign value for the metacolumn here

            aGrid.MetaDataTable.Rows.Add(arow)
        Catch ex As Exception
            MessageWindow.ShowMessage(ex, MsgBoxStyle.Information)
        End Try
    End Sub


    ''' <summary>
    ''' This subroutine calls  Metaquery Persons_PL_SaveToSociety 
    ''' and create an echo record of Person in Society 
    ''' the PersonID is the reference to Society.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>
    ''' Remember that an echo record to Society is not enforced by a relationship on the database
    ''' This is by design to permit many diferent entity to be link to Product and permit the modules of Order, Service, 
    ''' to be Fonctional and Factorizable for Vertical.
    '''  </remarks>
    Private Sub Persons_SaveToSociety(ByVal sender As Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim sqlcmd As System.Data.SqlClient.SqlCommand = Nothing
        Try
            'RUN on SystemBox_Master
            sqlcmd = MetaQuery.GetSPCmdByMetaName("Persons_PL_SaveToSociety")

            sqlcmd.Parameters.Clear()
            sqlcmd.Parameters.AddWithValue("@PersonID", aDataRow("PersonID"))
            sqlcmd.ExecuteReader(CommandBehavior.CloseConnection)

            MessageWindow.ShowMessage("Update to Society Done", MsgBoxStyle.Information)
        Catch sqlex As System.Data.SqlClient.SqlException
            Throw sqlex
        Catch ex As Exception
            Throw ex
        Finally
            If sqlcmd IsNot Nothing Then
                sqlcmd.Connection.Close()
                sqlcmd.Dispose()
            End If
        End Try



    End Sub







    ''' <summary>
    ''' This subroutine calls  Metaquery Persons_PL_SaveToProduct 
    ''' and create an echo record of Person in Product 
    ''' the PersonID is the reference to Product.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>
    ''' Remember that an echo record to product is not enforced by a relationship on the database
    ''' This is by design to permit many diferent entity to be link to Product and permit the modules of Configuration, Production, Invnetory and Quality
    ''' to be Fonctional and Factorizable for Vertical.
    '''  </remarks>
    Private Sub Persons_SaveToProduct(ByVal sender As Object, ByVal e As EventArgs)
        Dim aDataRow As DataRow = CType(CType(sender, DXMenuItem).Tag, DataRow)
        Dim sqlcmd As System.Data.SqlClient.SqlCommand = Nothing
        Try
            'RUN on SystemBox_Master
            sqlcmd = MetaQuery.GetSPCmdByMetaName("Persons_PL_SaveToProduct")

            sqlcmd.Parameters.Clear()
            sqlcmd.Parameters.AddWithValue("@PersonID", aDataRow("PersonID"))
            sqlcmd.ExecuteReader(CommandBehavior.CloseConnection)

            MessageWindow.ShowMessage("Update to Product Done", MsgBoxStyle.Information)
        Catch sqlex As System.Data.SqlClient.SqlException
            Throw sqlex
        Catch ex As Exception
            Throw ex
        Finally
            If sqlcmd IsNot Nothing Then
                sqlcmd.Connection.Close()
                sqlcmd.Dispose()
            End If
        End Try



    End Sub
End Class
